package com.cradom.event.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cradom.event.model.Account;
import com.cradom.event.model.Event;
import com.cradom.event.service.ChildrenService;
import com.cradom.event.service.EventService;

@Controller
@RequestMapping("/my")
public class UserController {
	
	@Resource
	private EventService eventService;
	
	@Resource
	private ChildrenService childrenService;
	
	/**
	 * 获取当前登录用户负责的事件
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("police/policeEvent")
	public String getEventByPolicePhone(HttpServletRequest request, HttpSession session) {
		Account account = (Account) session.getAttribute("admin");
		try {
			List<Event> eventList = eventService.getEventByPhone(account.getPhone());
			request.setAttribute("eventList", eventList);
			return "forward:/police/event.jsp";
		} catch (Exception e) {
			request.setAttribute("info", "session过期，请重新登录");
			return "forward:/error.jsp";
		}
	}
	
	/**
	 * 获取事件内容，用于事件更新form
	 * 
	 * @param request
	 * @param event_id
	 * @return
	 */
	@RequestMapping("police/toEventForm")
	public String toEventFrom(HttpServletRequest request, @RequestParam("event_id") String event_id) {
		Event event = eventService.getEventByID(event_id);
		request.setAttribute("event", event);
		return "forward:/police/eventForm.jsp";
	}
	
	/**
	 * 更新事件，如果finish内容不为空，则设置结案内容和结案时间
	 * 
	 * @param event
	 * @return
	 */
	@RequestMapping("police/updateEvent")
	public String updateEvent(Event event) {
		try {
			eventService.updateEvent(event);
			// 结案信息发布，则添加结案时间
			if (event.getFinish() != "" && event.getFinish() != null) {
				Date finish_time = new Timestamp(System.currentTimeMillis());
				eventService.setFinishTime(event.getId(), finish_time);
			}
		} catch (Exception e) {
			return "redirect:/error.jsp";
		}
		return "redirect:/my/police/policeEvent.do";
	}
	
	/**
	 * 获取当前登录用户负责的事件
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("user/userEvent")
	public String getEventByUserPhone(HttpServletRequest request, HttpSession session) {
		Account account = (Account) session.getAttribute("user");
		try {
			List<Event> eventList = eventService.getEventByPhone(account.getPhone());
			request.setAttribute("eventList", eventList);
			return "forward:/myEvent.jsp";
		} catch (Exception e) {
			request.setAttribute("info", "session过期，请重新登录");
			return "forward:/error.jsp";
		}
	}
	
	/**
	 * 已登录用户去报案页面
	 * @param request
	 * @return
	 */
	@RequestMapping("user/toChildrenEdit")
	public String toChildrenFrom(HttpServletRequest request){
		try{
			Account account=(Account) request.getSession().getAttribute("user");
			request.setAttribute("children", childrenService.getChildrenByUserID(account.getId()));
		}catch(Exception e){
			return "forward:/error.jsp";
		}
		return "forward:/children.jsp";
	}
}
